""" 游戏相关函数操作"""
import sys
import pygame
from pygame.sprite import Group
from Tower import Tower
from item import Item


def update_screen(screen, pySettings, towers, items, play_button,scoreBoard):
    """ 屏幕更新"""
    screen.fill(pySettings.bg_color)
    if pySettings.gameStar == False:
        play_button.draw_button()
    else:
        ''' 绘制tower'''
        for tower in towers:
            tower.draw_tower()
        
        for item in items:
            item.draw_item()
        scoreBoard.show_score()
        
    # 让最近绘制的屏幕可见
    pygame.display.flip()
    success(towers, pySettings)


def check_event(screen, pysettings, towers,play_button):
    """ 响应按键和鼠标事件 """
    # 如果不添加这个get,则界面会卡死
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            if pysettings.gameStar:
                chekc_mouseDown(screen, pysettings, towers, mouse_x, mouse_y)
            else:
                check_playbutton(play_button, pysettings, mouse_x, mouse_y)


def chekc_mouseDown(screen, pysettings, towers, mouse_x, mouse_y):
    """ 检查鼠标按下事件"""
    for tower in towers:
        if tower.rect.collidepoint(mouse_x, mouse_y):
            # 当前操作的是同一个塔
            if pysettings.currentItemTower == tower:
                item = tower.getLastItem()
                if item is None:
                    break
                if item.getChoose():  # 当前是选中方块，则还原
                    item.rect.y = tower.getCurrentY(item)
                    item.setChoose(False)
                    pysettings.currentItem = None
                else:
                    item.rect.y = tower.rect.y
                    item.setChoose(True)
                    pysettings.currentItemTower = tower
                    pysettings.currentItem = item
            else:  # 不是同一个塔
                currentItem = tower.getLastItem()
                if pysettings.currentItem is None:
                    if currentItem is None:
                        break
                    else:
                        currentItem.rect.y = tower.rect.y
                        currentItem.setChoose(True)
                        pysettings.currentItemTower = tower
                        pysettings.currentItem = currentItem
                else:
                    # 获取当前塔下的方块
                    item = pysettings.currentItemTower.getLastItem()
                    if currentItem is None or currentItem.rect.width > item.rect.width:
                        item.rect = pygame.Rect(tower.getNextItemPosition(item))
                        item.setChoose(False)
                        tower.addItem(item)
                        pysettings.currentItemTower.remove(item)
                        pysettings.currentItemTower = tower
                        pysettings.currentItem = None
                        record(pysettings)


def check_playbutton(play_button, pysettings, mouse_x, mouse_y):
    """检查按钮是否被按下"""
    if play_button.rect.collidepoint(mouse_x, mouse_y):
        pysettings.gameStar = True


def createTower(screen, pysettings):
    """ 添加3个塔 """
    towers = Group()
    towerA = Tower(screen, pysettings, 0, 00)
    towers.add(towerA)
    
    towerB = Tower(screen, pysettings, 210, 00)
    towers.add(towerB)
    
    towerC = Tower(screen, pysettings, 420, 00)
    towers.add(towerC)
    return towers


def createItem(screen, pysettings, tower, number):
    """ 创建方块 """
    items = Group()
    star = 0
    while star < number:
        item = Item(screen, pysettings, tower, pysettings.firstwidth - star * 20)
        items.add(item)
        star += 1
    return items


def success(towers, pysettings):
    """ 判断是否成功 第一 二个塔的方块数为0 """
    towerCopy = towers.sprites()
    if len(towerCopy[0].items) == 0 and len(towerCopy[1].items) == 0:
        print("success")

        towerCopy[0].items.clear()
        towerCopy[1].items.clear()
        towerCopy[2].items.clear()
        pysettings.level += 1
        pysettings.newGame = True
        pysettings.star = 0  # 重置记数器


def record(pysettings):
    pysettings.star += 1  #游戏步骤加1